<?php

/**
 * Description of users
 *
 * @author Lugol Metayer
 */

 
 class Users_Info extends Model {
                /*
                 * Retrieve all users from the database
                 */
		public function retrieve_users()
		{
		 
		 return $this->db->query('SELECT u.*, CASE WHEN u.permissions_1 = 0 THEN "Office Assistant" ELSE "Doctor" END AS permissions_1  FROM users u;')->result();
		 
		}
                //-------------------------------
                /*
                 * Check user name and password
                 * @param $where(for id of user)
                 */

		public function login($where)
		{
				$sql = "SELECT u.*  FROM users u where u.username = ?;";
				return $this->db->query($sql,array($where))->result();

			}
                        //-----------------------------
                        /*
                         * Retrieve details for a specific user
                         * @param $where(selects a user id)
                         */
		public function retrieve_details($where)
		{
				$sql = "SELECT u.*  FROM users u where u.user_id = ?;";
				return $this->db->query($sql,array($where))->result();
		}
                //------------------------------
                /*
                 * Insert a new user to the database
                 * @param $uname
                 * @param $pword
                 * @param $ans1 (security questions)
                 * @param $ans2 (security questions)
                 * @param $perm (permissions)
                 */
		public function insert_users($uname,$pword,$ans1,$ans2,$perm){
				$data = array('username'=>$uname, 'password'=>$pword, 'answer1'=>$ans1, 'answer2'=>$ans2, 'permissions'=>$perm);

				$str = $this->db->insert_string('users', $data);
				
				$query = $this->db->query($str);


		}
                //-------------------------------
                /*
                 * Update a record in the database
                 * * @param $uname
                 * @param $pword
                 * @param $ans1 (security questions)
                 * @param $ans2 (security questions)
                 * @param $perm (permissions)
                 */

		public function update_users($uname,$pword,$ans1,$ans2,$perm, $id){

				$data = array('username'=>$uname, 'password'=>$pword, 'answer1'=>$ans1, 'answer2'=>$ans2, 'permissions'=>$perm);

				$where = "user_id = $id";

				$str = $this->db->update_string('users', $data, $where);
				
				$query = $this->db->query($str);
		}
                //------------------------------
                /*
                 * Delete a record in the database
                 */
		public function delete_users($where){
				$sql = "DELETE FROM users where user_id = ?;";
				return $this->db->query($sql,array($where));
		}
//*EOF*//	
}	
